﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniLua;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            LuaFile.LUA_ROOT = "..\\LuaScripts";
            UniLua.Tools.ULDebug.Logger = new Logger();

            ILuaState ls = LuaAPI.NewState();
            ls.L_OpenLibs();

            while (true)
            {
                var cmd = Console.ReadLine();
                try
                {
                    var status = ThreadStatus.LUA_OK;

                    if (cmd.EndsWith(".lua"))
                        status = ls.L_DoFile(cmd);
                    else if (cmd == "clear")
                    {
                        ls = LuaAPI.NewState();
                        ls.L_OpenLibs();
                    }
                    else 
                        status = ls.L_DoString(cmd);

                    if (status != ThreadStatus.LUA_OK)
                    {
                        Console.WriteLine(ls.L_ToString(-1));
                        ls.Pop(1);
                    }
                    else
                        Console.WriteLine(status);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}
